package cn.edu.sjtu.se.dslab.component.design;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternBean;
import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternXMLParser;
import cn.edu.sjtu.se.dslab.helper.ApplicationContextHelpler;
import cn.edu.sjtu.se.dslab.service.IDesignPatternService;

/**
 * 
 * @author longshaohang
 * 
 */

@Entity
@Table(name = "design_pattern")
public class DesignPattern implements Serializable {
	
	public static final String DISPLAY_PAGE_ARINC661 = "arinc661";
	public static final String DISPLAY_PAGE_USERDEFINED = "userDefineDF";
	public static final String DISPLAY_PAGE_DATA = "pageData";

	private static final long serialVersionUID = 8598656097276859298L;

	private long id;
	private String name;
	private DesignPatternType type;
	private Date createTime;
	private String filePath;
	private String uri;
	private Set<DesignPatternProperty> properties;

	private static IDesignPatternService dpService;

	private static DesignPatternBean busDesignPatternBean;
	private static DesignPatternBean ioDesignPatternBean;
	private static DesignPatternBean displayDesignPatternBean;
	private static DesignPatternBean controlDesignPatternBean;

	public static DesignPatternBean getDesignPatternBean(DesignPatternType type) {
		// DesignPatternBean dpb = DesignPattern.class.getField(symbal +
		// "DesignPattern");
		if (dpService == null) {
			dpService = (IDesignPatternService) ApplicationContextHelpler
					.getBean("designPatternService");
		}
		switch (type) {
		case BUS:
			if (busDesignPatternBean == null) {
				DesignPattern dp = dpService
						.getDesignPatternByType(DesignPatternType.BUS);
				DesignPatternXMLParser parser = new DesignPatternXMLParser(
						dp.getFilePath());
				busDesignPatternBean = parser.parse();
			}
			return busDesignPatternBean;
		case IO:
			if (ioDesignPatternBean == null) {
				DesignPattern dp = dpService
						.getDesignPatternByType(DesignPatternType.IO);
				DesignPatternXMLParser parser = new DesignPatternXMLParser(
						dp.getFilePath());
				ioDesignPatternBean = parser.parse();
			}
			return ioDesignPatternBean;
		case CONTROL:
			if (controlDesignPatternBean == null) {
				DesignPattern dp = dpService
						.getDesignPatternByType(DesignPatternType.CONTROL);
				DesignPatternXMLParser parser = new DesignPatternXMLParser(
						dp.getFilePath());
				controlDesignPatternBean = parser.parse();
			}
			return controlDesignPatternBean;
		case DISPLAY:
			if (displayDesignPatternBean == null) {
				DesignPattern dp = dpService
						.getDesignPatternByType(DesignPatternType.DISPLAY);
				DesignPatternXMLParser parser = new DesignPatternXMLParser(
						dp.getFilePath());
				displayDesignPatternBean = parser.parse();
				
			}
			return displayDesignPatternBean;
		}
		return null;
	}

	/**
	 * getter and setter
	 */

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@Column(nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, updatable = false, name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Enumerated(EnumType.ORDINAL)
	public DesignPatternType getType() {
		return type;
	}

	public void setType(DesignPatternType type) {
		this.type = type;
	}

	@Column(nullable = false)
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	@Column(nullable = false)
	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "designPattern", fetch = FetchType.EAGER)
	public Set<DesignPatternProperty> getProperties() {
		return properties;
	}

	public void setProperties(Set<DesignPatternProperty> properties) {
		this.properties = properties;
	}

}
